<?php 
	include 'includes/conectionMySql.inc';
	// Variável de conexão: $db;
	include 'includes/security.php';
	
	$_POST = validaParametro($_POST);
	
	$valida = $_POST["envia"];

	if ($valida != 1){
		echo "Erro de validação"; 
		exit();
	}
	if($_POST['txtAdmissoes']==''||$_POST['txtContratacoes']==''||$_POST['txtDesligamentos']==""){
		echo "Erro";
		exit();
	}
	
	$emailsAdmissoes = explode(" ",$_POST['txtAdmissoes']);
	$emailsContratacoes = explode(" ",$_POST['txtContratacoes']);
	$emailsDesligamentos = explode(" ",$_POST['txtDesligamentos']);	
	
	foreach ($emailsAdmissoes as $email){
		if(filter_var($email,FILTER_VALIDATE_EMAIL)==false){
			echo "Erro";
			exit();
		}
	}
	foreach ($emailsContratacoes as $email){
		if(filter_var($email,FILTER_VALIDATE_EMAIL)==false){
			echo "Erro";
			exit();
		}
	}
	foreach ($emailsDesligamentos as $email){
		if(filter_var($email,FILTER_VALIDATE_EMAIL)==false){
			echo "Erro";
			exit();
		}
	}
	
	$query = "TRUNCATE TABLE email_config";
	$result = mysql_query($query);
		
	foreach ($emailsAdmissoes as $email){
		$query = 'INSERT INTO email_config (email,nivel_email) VALUES (\''.$email.'\',1)';
		$result = mysql_query($query);
	}
	
	foreach ($emailsDesligamentos as $email){
		$query = 'INSERT INTO email_config (email,nivel_email) VALUES (\''.$email.'\',2)';
		$result = mysql_query($query);
	}
	
	foreach ($emailsContratacoes as $email){
		$query = 'INSERT INTO email_config (email,nivel_email) VALUES (\''.$email.'\',3)';
		$result = mysql_query($query);
	}
	if(mysql_affected_rows($db)>0){
		header("Location: dummy.php?x=1");
	}else{
		header("Location: dummy.php?x=2");
	}
?>